﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace DevDemo
{
    public partial class ListViewDemo : Form
    {
        public ListViewDemo()
        {
            InitializeComponent();

            LoadView();
        }

        public void LoadView()
        {
            this.listView1.BeginUpdate();

            for (int i = 0; i < 5; i++)
            {
                ColumnHeader ch = new ColumnHeader();

                ch.Width = 70;

                ch.Text = "Col" + i;

                ch.TextAlign = HorizontalAlignment.Center;

                this.listView1.Columns.Add(ch);
            }

            this.listView1.EndUpdate();
        }

        /// <summary>
        /// add_rows
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            //string[] str_arr = { "1", "2", "3", "4", "5" };

            //ListViewItem vitem = new ListViewItem(str_arr);

            //this.listView1.Items.Add(vitem);

            ListViewItem rowItem = new ListViewItem();

            ListViewItem.ListViewSubItem subitem = null;

            foreach (ColumnHeader item in this.listView1.Columns)
            {
                subitem = new ListViewItem.ListViewSubItem();

                subitem.Text = item.Text;

                rowItem.SubItems.Insert(item.Index, subitem);
            }

            this.listView1.Items.Add(rowItem);
        }

        /// <summary>
        /// del rows
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            if (this.listView1.Items.Count > Convert.ToInt32(this.textBox1.Text.Trim()))
            {
                this.listView1.Items.RemoveAt(Convert.ToInt32(this.textBox1.Text.Trim()));
            }
        }

        /// <summary>
        /// add columns
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            ColumnHeader ch = new ColumnHeader();

            ch.Text = "Col";

            ch.TextAlign = HorizontalAlignment.Center;

            this.listView1.Columns.Insert(0, ch);

            foreach (ListViewItem item in this.listView1.Items)
            {
                item.SubItems.Insert(0, new ListViewItem.ListViewSubItem { Text = "1"});
            }
        }

        /// <summary>
        /// del columns
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            string colIndex = this.textBox1.Text.Trim();

            if (Convert.ToInt32(colIndex) <= this.listView1.Columns.Count)
            {
                this.listView1.Columns.RemoveAt(Convert.ToInt32(colIndex));

                foreach (ListViewItem item in this.listView1.Items)
                {
                    item.SubItems.RemoveAt(Convert.ToInt32(colIndex));
                }
            }

            if (listView1.Columns.Count == 0)
            {
                this.listView1.Items.Clear();
            }
        }

        /// <summary>
        /// 修改值 x,y 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tb_value_TextChanged(object sender, EventArgs e)
        {
            if (tb_x.Text.Trim().Length > 0 && tb_y.Text.Trim().Length > 0 && tb_value.Text.Trim().Length > 0)
            {
                if (this.listView1.Items.Count > Convert.ToInt32(tb_x.Text.Trim()) && this.listView1.Columns.Count > Convert.ToInt32(tb_y.Text.Trim()))
                {
                    ListViewItem Row = this.listView1.Items[Convert.ToInt32(tb_x.Text.Trim()) - 1];

                    ListViewItem.ListViewSubItem ite = Row.SubItems[Convert.ToInt32(tb_y.Text.Trim()) - 1];

                    this.listView1.Items[Convert.ToInt32(tb_x.Text.Trim()) - 1].SubItems[Convert.ToInt32(tb_y.Text.Trim()) - 1].Text = tb_value.Text.Trim();
                }

            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (this.comboBox1.SelectedItem.ToString())
            {
                case "LargeIcon":
                    this.listView1.View = View.LargeIcon;

                    break;
                case "Details":
                    this.listView1.View = View.Details;

                    break;
                case "SmallIcon":
                    this.listView1.View = View.SmallIcon;

                    break;
                case "List":
                    this.listView1.View = View.List;

                    break;
                case "Tile":
                    this.listView1.View = View.Tile;

                    break;
            }
        }
    }
}
